package com.liupengyuan.system.user.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liupengyuan.framework.web.query.IGenericPage;
import com.liupengyuan.system.user.dao.UserDao;
import com.liupengyuan.system.user.model.User;
import com.liupengyuan.system.user.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
	public List<User> findListBy(User user,String sort,String dir) {
		return userDao.findListBy(findParam(user),sort,dir);
	}

	public IGenericPage findPageBy(User user, int pageNo, int pageSize,
			String sort, String dir) {
		return userDao.findPageBy(findParam(user), pageNo, pageSize, sort, dir);
	}

	public User getById(String id) {
		return (User) userDao.getById(id);
	}

	public Integer save(User user) {
		return userDao.save(user);
	}

	public Integer update(User user){
		return userDao.update(user);
	}

	public Integer deleteByIds(String[] ids) {
		return userDao.deleteByIds(ids);
	}
	public Integer deleteById(String id) {
		return userDao.deleteById(id);
	}

	public Integer getCountByParam(User user) {
		return userDao.getCountBy(user);
	}

	public Integer saveOrUpdate(User user) {
		if(StringUtils.isNotEmpty(user.getId())){
			return userDao.update(user);
		}else{
			return userDao.save(user);
		}
	}
	
	@Override
	public User login(User user){
		return userDao.login(user);
	}
	
	private User findParam(User user){
		if(StringUtils.isNotEmpty(user.getName())){
			user.setName("%" + user.getName() + "%");
		}
		if(StringUtils.isNotEmpty(user.getUsername())){
			user.setUsername("%" + user.getUsername() + "%");
		}
		return user;
	}
}
